package com.sleepingcat.ft_flutter.fragment

import android.app.Activity
import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import com.cat.android.lib_base.base.RouterFragment
import com.cat.android.lib_ui.ext.onClick
import com.cat.android.lib_ui.widget.toast.showText
import com.cat.android.plugin_navigation_runtime.NavDestination
import com.idlefish.flutterboost.containers.FlutterActivityLaunchConfigs
import com.sleepingcat.ft_flutter.channel.ChannelManager
import com.sleepingcat.lib_common.const.Routes
import com.sleepingcat.lib_common.service.IFlutter
import com.sleepingcat.lib_flutter.databinding.FragmentBoostTestBinding
import kotlinx.coroutines.launch


/**
 * @作者 志浩
 * @时间 2025/5/2 12:16
 * @描述 TODO
 */
@NavDestination(type = NavDestination.NavType.Fragment, route = Routes.Flutter.BOOST_TEST)
class BoostTestFragment : RouterFragment<FragmentBoostTestBinding>() {
    private val TAG = "BoostTestFragment"
    private val boostMainPageRoute = "_boostStart/boostMainPage"

    private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(), ActivityResultCallback<ActivityResult> {
        val data: Intent? = it.data
        val resultCode: Int = it.resultCode
        if (resultCode == Activity.RESULT_OK) {
            val result: HashMap<String, Any>? = data?.getSerializableExtra(FlutterActivityLaunchConfigs.ACTIVITY_RESULT_KEY) as? HashMap<String, Any>
            val string = result?.get("name") ?: "什么都没有"
            "收到flutter端返回的结果:$string".showText()
        }
    })

    override fun initView() {
        super.initView()
        binding.apply {
            btnBoostMainPage.onClick {
                navigateWithResult(Routes.SingleFlutterFragment.SINGLE_FLUTTER_FRAGMENT.hashCode(), "111", args = MyFlutterFragment.createArgument(uniqueId = null, url = boostMainPageRoute, "111", urlParams = hashMapOf("name" to "cat"))) { code, bundle ->
                    "收到flutter返回的结果:${bundle.getString("name")}".showText()
                }
            }
            btnFlutterMsgDialog.onClick {
                ChannelManager.dialogChannel.flutterApi.showFlutterNormalDialog("FlutterMsgDialog", "原生启动且创建新Act") {
                    "flutter点击的是：$it".showText()
                }
            }
            btnFlutterInputDialog.onClick {
                ChannelManager.dialogChannel.flutterApi.showFlutterInputDialog("FlutterInputDialog", "原生启动且创建新Act", "请输入内容") {
                    "flutter输入框内容是：$it".showText()
                }
            }
            btnFlutterMsgDialogByFragment.onClick {
                // 通过fragment打开Flutter的dialog，不建议原生通过fragment打开透明的flutterDialog，因为透明的非DialogFragment动画不流畅，安卓问题，
                // 消息弹窗禁止动画呈现效果还可以，但如果是输入弹窗会有软键盘顶起dialog的动画，一有动画又废了

                // 但是反过来flutter打开原生的dialog就没问题，可能DialogFragment的动画实现不一样
                // 如果想通过fragment启动flutter的dialog，那flutter那边要提供对应的路由注册
                lifecycleScope.launch {
                    val result = IFlutter.get()?.showFlutterMsgDialog("FlutterDialog", "原生启动（Fragment）")
                    "flutter点击的是：$result".showText()
                }
            }
            btnFlutterInputDialogByFragment.onClick {
                // 不建议使用原生通过fragment启动flutter透明dialog
                lifecycleScope.launch {
                    val result = IFlutter.get()?.showFlutterInputDialog("FlutterDialog", "原生启动（Fragment）", "请输入内容")
                    "flutter输入框内容是：$result".showText()
                }
            }
            btnFlutterReturnOriginal.onClick {
                IFlutter.get()?.navigateForResult(requireContext(), boostMainPageRoute, hashMapOf("name" to "cat"), true, launcher)
            }
            btnFlutterReturnPigeon.onClick {
                ChannelManager.boostChannel.flutterApi.jumpToBoostMainPageAndReturnValue {
                    val name = it["name"]
                    "收到flutter端返回的结果:$name".showText()
                }
            }
        }
    }
}